<template>
  <div class="view-fill">
    <div v-if="!modifyInfo.visible" class="app-container-flex">
      <NewFullScreen
        v-model="showDialog"
        :title="title"
        :needOperateAffair="true"
        :operateAffairData="affairTableInfo"
        @returnBack="$emit('returnBack')"
      >
        <template v-slot:full-button>
          <PopoverPrint
            ref="popoverShow"
            v-model:popoverVisible="popoverVisible"
            :canPrint="canPrint"
            :templateArr="templateArr"
            @tempClick="tempClick"
            @print="print"
          ></PopoverPrint>
          <!-- <el-button
            v-if="$permission('withTableAcceptOrder:printOrder')"
            @click="print"
            >打印</el-button
          > -->
          <el-button
            v-if="$permission('withTableAcceptOrder:excelOrder')"
            @click="exportExcel"
            >导出excel</el-button
          >
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="infoList"></basic-information>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('detailInfo') }}</p>
          </div>
          <div class="model-panel-body">
            <div class="layui-card-body">
              <!-- 表格 -->
              <page-table
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :field-list="tableInfo.fieldList"
                :handle="tableInfo.handle"
                :check-box="false"
                :tab-index="true"
                :isDynamic="true"
              >
              </page-table>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <individual-details
      v-if="individualInfo.visible"
      v-model:show-dialog="individualInfo.visible"
      :query="individualInfo.query"
      :info="individualInfo.info"
    />
  </div>
</template>

<script setup name="details">
import BasicInformation from '@/components/BasicInformation/index'
import PageTable from '@/components/PageTable/index'
import IndividualDetails from './IndividualDetails'
import { getCustomizeSetting } from '@/api/customize'
import {
  getWithTableAcceptOrderById,
  getWithTableAcceptOrderDetailAll,
  withTableAcceptOrderPrintOrder,
  withTableAcceptOrderExcelOrder,
} from '@/api/withTable.js'
import { fatchTemplateByCode } from '@/api/base'
import { orderOpAffairAll } from '@/api/affair'
import { doPrint } from '@/utils/exportUtil.js'
import { selectByCodeAndDept } from '@/api/report.js'
import { updatePrintCount } from '@/api/mapping'

const { proxy } = getCurrentInstance()
const emit = defineEmits()
const props = defineProps(['visible', 'id'])

const title = ref('跟台验收记录详情')
const infoList = reactive({
  data: {},
  fieldList: [
    {
      label: '单号',
      value: 'orderNo',
    },
    {
      label: '跟台订单号',
      value: 'withTableOrderNo',
    },
    {
      label: '验收状态',
      value: 'acceptStatusValue',
    },
    {
      label: '验收时间',
      value: 'acceptTime',
    },
    {
      label: '验收人',
      value: 'acceptUserName',
    },
    {
      label: '使用状态',
      value: 'useStatusValue',
    },
    {
      label: '备注',
      value: 'remark',
      type: 'popover',
    },
  ],
})
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '80',
    btList: [
      {
        label: '个体详情',
        event: showDetail,
        show: proxy.$permission('withTableAcceptOrder:individualDetails'),
      },
    ],
  },
})
const filterInfo = reactive({
  query: {
    customizeUserVO: {},
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: props.id,
      },
    },
    queryParams: {},
  },
})
const affairTableInfo = reactive({
  api: orderOpAffairAll,
  query: {
    orderType: 80,
    orderId: props.id,
    sort: 'operate_time desc',
  },
})
const modifyInfo = reactive({
  visible: false,
  orderId: '',
  title: '修改跟台记录',
})
const printerInfo = reactive({
  visible: false,
  data: {},
})
const individualInfo = reactive({
  visible: false,
  query: {},
  info: {},
})
const canPrint = computed(() => {
  return proxy.$permission('withTableAcceptOrder:printOrder')
})
const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emit('update:visible', newValue) // 更新父组件visible
  },
})

let obj = {
  preQty: {
    label: '已使用数量',
    value: 'preQty',
    type: 'column',
    componentType: 'column',
    formatter: proxy.$fn.qtyFixedFormatter,
    show: false,
    hidden: true,
  },
}
tableInfo.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
})
getCustomize()

//导出excel
async function exportExcel() {
  withTableAcceptOrderExcelOrder(filterInfo.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
const templateArr = ref([])
const popoverVisible = ref(false)
//打印
function print() {
  selectByCodeAndDept({
    tempCode: 'withTableAcceptDetail',
    deptId: infoList.data.deptId,
  }).then((res) => {
    if (res && res.success) {
      templateArr.value = res.data
      popoverVisible.value = true
      // 一条数据则直接打印
      if (templateArr.value.length === 1) {
        proxy.$refs.popoverShow.hidePopover()
        doPrint({
          name: templateArr.value[0].ureportName,
          params: {
            orderId: props.id,
          },
          callback: printCallback,
          data: {},
        })
      }
    }
  })
}
function printCallback() {
  try {
    // 打印成功增加打印次数
    updatePrintCount({
      orderNo: infoList.data.orderNo,
      orderId: props.id,
      orderType: 80,
    })
  } catch (error) {}
}
function tempClick(item) {
  doPrint({
    name: item.ureportName,
    params: {
      orderId: props.id,
    },
    callback: printCallback,
    data: {},
  })
  popoverVisible.value = false
}
function getPrintData(temp) {
  let params = {
    tableKey:
      'withTableAcceptOrderDetails-com.slip.spd.entity.vo.withtable.WithTableAcceptOrderDetailVO',
    orderIds: [props.id],
  }
  withTableAcceptOrderPrintOrder(params).then((res) => {
    if (res.success && res) {
      let data = proxy.$fn.deepClone(res.data)
      let newData = data.map((item) => {
        return {
          ...item,
          ...item.orderInfo,
        }
      })
      proxy.$lodop.preview(temp, newData)
    }
  })
}
function showDetail(row) {
  //个体详情
  let ids
  if (row.groupConcatIds) {
    //未定义表示没有分组条件
    ids = row.groupConcatIds
  } else {
    ids = '' + row.id
  }
  individualInfo.query = {
    id: {
      queryType: 'IN',
      queryCategory: 'AND',
      value: ids,
    },
    orderId: {
      queryType: 'EQUALS',
      queryCategory: 'AND',
      value: props.id,
    },
  }
  individualInfo.info = row
  individualInfo.visible = true
}
function getList() {
  getWithTableAcceptOrderDetailAll(filterInfo.query).then((resp) => {
    if (resp && resp.success) {
      tableInfo.data = resp.data
    }
  })
}

function getMain() {
  getWithTableAcceptOrderById(props.id).then((resp) => {
    if (resp && resp.success) {
      infoList.data = resp.data
    }
  })
}
function returnModify() {
  getMain()
  getList()
}
function getCustomize() {
  let param = {
    tableKey:
      'withTableAcceptOrderDetails-com.slip.spd.entity.vo.withtable.WithTableAcceptOrderDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.query.customizeUserVO = data
      let obj = proxy.$fn.setFieldList(tableInfo.fieldList, data.detail, true)
      tableInfo.fieldList = obj.fieldList
      getList()
      getMain()
    }
  })
}
</script>

<style scoped></style>
